<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/4
 * Time: 15:12
 */

namespace app\sports_admin\controller;
use app\exception\ChildrenException;
use app\sports_admin\model\ChildrenHeight;
use app\sports_admin\model\ChildrenWeight;
use think\Request;
use app\sports_admin\service\Children as ChildrenService;

class Children extends Base
{
    //身高配置页面
    public function height()
    {
        return view("height");
    }
    //身高列表
    public function heightList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $result = ChildrenHeight::heightList($page,$rows,$sort,$sortOrder);
        return json($result);
    }
    //添加身高
    public function heightAdd(Request $request)
    {
        if($request->isAjax()){
            $data = ChildrenService::heightAdd();
            if($data){return json(['code'=>"200","msg"=>"添加成功"]);
            }else if ($data == false){
                return json(['errorCode'=>"400","msg"=>"重复的身高"]);
            }else{
                throw new ChildrenException(['msg'=>'添加失败', 'errorCode'=>"404"]);
            }
        }
    }
    //删除身高
    public function heightDel(Request $request)
    {
        if($request->isAjax()){
            $data = ChildrenHeight::heightDel();
            if($data){
                return json(['code'=>"200","msg"=>"删除成功"]);
            }else{
                throw new ChildrenException(['msg'=>'删除失败', 'errorCode'=>"404"]);
            }
        }
    }
    //体重配置页面
    public function weight()
    {
        return view("weight");
    }
    //体重列表
    public function weightList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $result = ChildrenWeight::weightList($page,$rows,$sort,$sortOrder);
        return json($result);
    }
    //添加体重
    public function weightAdd(Request $request)
    {
        if($request->isAjax()){
            $data = ChildrenService::weightAdd();
            if($data){return json(['code'=>"200","msg"=>"添加成功"]);
            }else if ($data == false){
                return json(['errorCode'=>"400","msg"=>"重复的体重"]);
            }else{
                throw new ChildrenException(['msg'=>'添加失败', 'errorCode'=>"404"]);
            }
        }
    }
    //删除体重
    public function weightDel(Request $request)
    {
        if($request->isAjax()){
            $data = ChildrenWeight::weightDel();
            if($data){
                return json(['code'=>"200","msg"=>"删除成功"]);
            }else{
                throw new ChildrenException(['msg'=>'删除失败', 'errorCode'=>"404"]);
            }
        }
    }
}